package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;


import com.sevenstudio.adu.gasing.bluetooth.BT_Server_Client;
import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;

import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.define.iServer_Data;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.engine.GameEngine;

public class State_Game_Pause implements iGameDefines
{
	ASprite		bgMenu, btnResume, btnRestart, btnMenu, icon, ekspresi, text;
	AScene		scene;
	GameEngine	engine;
	public static boolean isResume, isRestart, isMenu;
	
	public State_Game_Pause(GameEngine engine)
	{
		this.engine = engine;
		
		bgMenu = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_BG);
		btnResume = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_RESUME);
		btnRestart = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_RESTART);
		btnMenu = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_MENU);
		ekspresi = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_EKSPRESI_PAUSE);
		text = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_TEXT_PAUSE);
	}
	
	public void Init()
	{
		isResume = isRestart = isMenu = false;
		
		scene = engine.getGameScene();

		if(BT_Server_Client.isBluetooth && !BT_Server_Client.isServer)
			icon = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_ACEH + iServer_Data.INDEX_RIVAL);
		else if(GameEngineConfiguration.ModeSelected == MODE_CAREER)
			icon = new ASprite(engine, SPRITE_MC_CAREER, DEFAULT_GASING_PAUSE);
		else
			icon = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_ACEH + iServer_Data.INDEX_MC);

		
		
		State_Game_Play.resume = true;
		
		btnRestart.SetPos(SPRITE_GAME[SPRITE_GAME_BTN_RESTART].getX(),
				SPRITE_GAME[SPRITE_GAME_BTN_RESTART].getY(),
				SPRITE_GAME[SPRITE_GAME_BTN_RESTART].getAnchor());
		btnMenu.SetPos(SPRITE_GAME[SPRITE_GAME_BTN_MENU].getX(),
				SPRITE_GAME[SPRITE_GAME_BTN_MENU].getY(),
				SPRITE_GAME[SPRITE_GAME_BTN_MENU].getAnchor());
		
//		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bgMenu);
		engine.SceneAttach(scene, btnResume);
		engine.SceneAttach(scene, btnRestart);
		engine.SceneAttach(scene, btnMenu);
		engine.SceneAttach(scene, icon);
		engine.SceneAttach(scene, ekspresi);
		engine.SceneAttach(scene, text);

		scene.theScene().registerTouchArea(btnResume.theSprite());
		scene.theScene().registerTouchArea(btnRestart.theSprite());
		scene.theScene().registerTouchArea(btnMenu.theSprite());
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
	}
	
	public void Update()
	{	
		if(isMenu)
		{
			isMenu = false;
			menu();

//			if(BT_Server_Client.isBluetooth)
//			BT_Server_Client.stopBTThread();
		}	
		if(isRestart)
		{
			isRestart = false;
			restart();
		}	
		if(isResume)
		{
			isResume = false;
			resume();
		}
	}
	
	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnResume.theSprite()))
		{
			resume();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("resume");
			}
		}
		else if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnRestart.theSprite()))
		{
			restart();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("restart");
			}
			
		}
		else if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnMenu.theSprite()))
		{
			menu();	
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("menu");
			}
		}
		
	}
	
	public void keyBack(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{	
			resume();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("resume");
			}	
		}
		
	}
	
	private void resume()
	{
		final EngineLock engineLock = engine.getEngine().getEngineLock();
		engineLock.lock();
		
		bgMenu.DetachSelf();
		btnResume.DetachSelf();
		btnRestart.DetachSelf();
		btnMenu.DetachSelf();
		icon.DetachSelf();
		ekspresi.DetachSelf();
		text.DetachSelf();
		
		engineLock.unlock();
		engine.SetNextMessageState(1);
		engine.SetNextGameState(GAME_STATE_GAMEPLAY);	
	}
	
	private void restart()
	{
		final EngineLock engineLock = engine.getEngine().getEngineLock();
		engineLock.lock();

		State_Game_Play.reset();
		scene.theScene().detachChildren();

		engineLock.unlock();
		engine.SetNextMessageState(0);
		engine.SetNextGameState(GAME_STATE_GAMEPLAY);
	}
	
	private void menu()
	{
		final EngineLock engineLock = engine.getEngine().getEngineLock();
		engineLock.lock();

		State_Game_Play.reset();
		scene.theScene().detachChildren();

		engineLock.unlock();

		engine.getEngine().disableAccelerationSensor(engine);
		engine.SetNextMessageState(0);
		engine.SetNextGameState(GAME_STATE_MAINMENU);
	}
}
